import { ref, shallowRef, watch } from 'vue';
import { getTrafficRoad } from '@/api/tomae';

/**
 * 区域和道路选择及数据管理
 */
export function useNodeAndRoad() {
  const areaOptions = shallowRef([
    { value: '9110', label: '行政总区域' },
    { value: '9317', label: '兰山区' },
    { value: '9318', label: '罗庄区' },
    { value: '9353', label: '河东区' },
    { value: '9319', label: '沂南县' },
    { value: '9320', label: '郯城县' },
    { value: '9321', label: '沂水县' },
    { value: '9322', label: '兰陵县' },
    { value: '9323', label: '费县' },
    { value: '9324', label: '平邑县' },
    { value: '9325', label: '莒南县' },
    { value: '9326', label: '蒙阴县' },
    { value: '9327', label: '临沭县' },
  ]);
  const defaultNodeId = areaOptions.value[0].value;
  // 区域Id
  const nodeId = ref(defaultNodeId);
  // 道路Id
  const roadId = ref(undefined);
  const roadList = ref([]);

  const getRoadList = () => {
    //这个接口废掉了  没有用到
    // getTrafficRoad({ nodeId: nodeId.value }).then((res) => {
    //   roadList.value = res.result.list;
    // });
  };

  const reset = () => {
    nodeId.value = defaultNodeId;
    // 重置时，若nodeId就是默认值，不会触发watch, 手动重置roadId
    roadId.value = undefined;
  };

  watch(
    nodeId,
    (val) => {
      if (!val) {
        roadList.value = [];
      } else {
        getRoadList();
      }
      // 只要区域变了，道路就要重置，两级联动
      roadId.value = undefined;
    },
    { immediate: true },
  );

  return {
    nodeId,
    roadId,
    roadList,
    areaOptions,
    reset,
  };
}
